# 1, 操作
# 业务1：登录->用户分组—>添加分组->删除分组
# 业务2：登录->客服->客服话术->添加话术->删除话术
# 业务3：登录->商品->商品分类->添加分类->删除分类
# 2、使用pytest框架做，要有setup和teardown
import pytest
import requests
import uuid,allure
uuid1 = str(uuid.uuid4()).split('-')[0]
from faker import Faker
@allure.feature("用户分组")
class TestCRMEB:
    project_url = "http://60.205.190.66:8899"
    faker = Faker(locale='zh_cn')
    name = faker.name()
    @pytest.fixture(scope='function', autouse=True)
    @pytest.mark.mark
    def business(self):
        response1 = requests.post(url=self.project_url + "/adminapi/login",
                                  json={"account": "admin", "pwd": "920509", "imgcode": "a"})
        self.token = response1.json()['data']['token']
        print(self.token)
        print("登录成功")
        yield
        print("退出登录")

    @allure.story("添加分组")
    def test_1(self):
        response2 = requests.post(url=self.project_url + "/adminapi/user/user_group/save.html",
                                  json={"group_name": self.name},
                                  headers={"Authori-Zation": "Bearer " + str(self.token)})
        assert response2.json()['msg'] == "提交成功！"

    @allure.story("删除分组")
    def test_2(self):
        response3 = requests.get(url=self.project_url + "/adminapi/user/user_group/list?page=1&limit=15",
                                 headers={"Authori-Zation": "Bearer " + str(self.token)})
        for i in response3.json()['data']['list']:
            if i['group_name'] == self.name:
                self.id = i['id']
        print(response3.json())
        response4 = requests.delete(url=self.project_url + "/adminapi/user/user_group/del/" + str(self.id),
                                    headers={"Authori-Zation": "Bearer " + str(self.token)})
        assert response4.json()['msg'] == "删除成功!"
        print("删除成功!")

    @allure.feature("客服话术")
    @allure.story("添加话术")
    def test_3(self):
        response1 = requests.post(self.project_url + "/adminapi/app/wechat/speechcraft",
                                  json={"cate_id": "", "title": "赵四舞蹈的玄妙1",
                                        "message": str(uuid1), "sort": 0},
                                  headers={"Authori-Zation": "Bearer " + str(self.token)})
        assert response1.status_code == 200
        assert response1.json()['msg'] == "创建话术成功"

        # 查询
        response2 = requests.get(self.project_url + "/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=",
                                 headers={"Authori-Zation": "Bearer " + str(self.token)})
        assert response2.status_code == 200
        for i in response2.json()['data']['list']:
            if i['message'] == str(uuid1):
                self.id = i['id']

        # 编辑
        response3 = requests.put(self.project_url + "/adminapi/app/wechat/speechcraft/" + str(self.id),
                                 headers={"Authori-Zation": "Bearer " + str(self.token)},
                                 json={"cate_id": 0, "title": "赵四舞蹈的玄妙1", "message": "小天才", "sort": 0})
        assert response3.status_code == 200
        assert response3.json()['msg'] == "修改成功"

        # 删除
        response4 = requests.delete(self.project_url + "/adminapi/app/wechat/speechcraft/" + str(self.id),
                                    headers={"Authori-Zation": "Bearer " + str(self.token)})
        assert response4.status_code == 200
        assert response4.json()['msg'] == "删除成功"

    @allure.feature("商品")
    def test_4(self):
        # 新增
        response1 = requests.post(self.project_url + "/adminapi/product/category.html",
                                  headers={"Authori-Zation": "Bearer " + str(self.token)},
                                  json={"pid": 14, "cate_name": "小天才给大家表演" + str(uuid1), "pic": "", "big_pic": "",
                                        "sort": 0, "is_show": 1})
        assert response1.status_code == 200
        assert response1.json()['msg'] == '添加分类成功!'

        # 查询 ?pid=&is_show=&page=1&cate_name=
        response2 = requests.get(self.project_url + "/adminapi/product/category",
                                 params={"page": 1, "cate_name": "小天才给大家表演" + str(uuid1)},
                                 headers={"Authori-Zation": "Bearer " + str(self.token)})
        print(response2.json())
        assert response2.status_code == 200
        for i in response2.json()['data']['list'][0]['children']:
            if i['cate_name'] == "小天才给大家表演" + str(uuid1):
                self.id = i['id']

        # 删除
        response3 = requests.delete(self.project_url + "/adminapi/product/category/" + str(self.id),
                                    headers={"Authori-Zation": "Bearer " + str(self.token)})
        assert response3.json()['msg'] == "删除成功!"
        assert response3.status_code == 200


if __name__ == "__main__":
    pytest.main()